/*
 * 常量配置
 * @Author: Duyb
 * @Date: 2021-12-01 15:40:41
 * @Last Modified by:   Duyb
 * @Last Modified time: 2021-12-01 15:40:41
 */
export const USER = 'USER';

export const bytesToSize = (bytes: number) => {
  if (bytes === 0) return '0 B';
  const k = 1000; // or 1024
  const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i];
}

export const copy = (text: string) => {
  const input = document.createElement('input');
  document.body.appendChild(input);
  input.setAttribute('value', text);
  input.select();
  if (document.execCommand('copy')) {
    document.execCommand('copy');
  }
  document.body.removeChild(input)
}

export const formatDate = (value: number) => {
  const date = new Date(value * 1000);
  const y = date.getFullYear();
  let MM = date.getMonth() + 1 as number;
  MM = MM < 10 ? ('0' + MM) as unknown as number : MM;
  let d = date.getDate();
  d = d < 10 ? ('0' + d) as unknown as number : d;
  let h = date.getHours();
  h = h < 10 ? ('0' + h) as unknown as number : h;
  let m = date.getMinutes();
  m = m < 10 ? ('0' + m) as unknown as number : m;
  let s = date.getSeconds();
  s = s < 10 ? ('0' + s) as unknown as number : s;
  return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
};

export const friendlyDate = (timestamp: number) => {
  const formats = {
    'year': '%n% 年前',
    'month': '%n% 月前',
    'day': '%n% 天前',
    'hour': '%n% 小时前',
    'minute': '%n% 分钟前',
    'second': '%n% 秒前',
  };

  const now = Date.now();
  let seconds = Math.floor((now / 1000 - timestamp));
  const minutes = Math.floor(seconds / 60);
  const hours = Math.floor(minutes / 60);
  const days = Math.floor(hours / 24);
  const months = Math.floor(days / 30);
  const years = Math.floor(months / 12);

  let diffType = '';
  let diffValue = 0;
  if (years > 0) {
    diffType = 'year';
    diffValue = years;
  } else {
    if (months > 0) {
      diffType = 'month';
      diffValue = months;
    } else {
      if (days > 0) {
        diffType = 'day';
        diffValue = days;
      } else {
        if (hours > 0) {
          diffType = 'hour';
          diffValue = hours;
        } else {
          if (minutes > 0) {
            diffType = 'minute';
            diffValue = minutes;
          } else {
            diffType = 'second';
            diffValue = seconds === 0 ? (seconds = 1) : seconds;
          }
        }
      }
    }
  }
  // @ts-ignore
  return formats[diffType].replace('%n%', diffValue);
}

// 列表时间
export function formatSimpleDateTime(unixStamp: number) {
  unixStamp *= 1000
  const _today_obj = new Date()
  const _today_date = {
    y: _today_obj.getFullYear(),
    m: (_today_obj.getMonth() + 1 < 10 ? '0' + (_today_obj.getMonth() - -1) : (_today_obj.getMonth() - -1)),
    d: (_today_obj.getDate() < 10 ? '0' + _today_obj.getDate() : _today_obj.getDate())
  }

  const _today_stamp = Date.parse(_today_date.y + '/' + _today_date.m + '/' + _today_date.d + ' 00:00:00')

  const stamp = []
  stamp[0] = _today_stamp + 86400000
  stamp[1] = _today_stamp
  stamp[2] = _today_stamp - 86400000
  stamp[3] = _today_stamp - 172800000

  stamp[4] = _today_stamp - 518400000 // 7天

  stamp[5] = _today_stamp - 31536000000 // 365天

  const _compare_obj = new Date()
  _compare_obj.setTime(unixStamp)

  let return_str

  if (unixStamp >= stamp[1] && unixStamp < stamp[0]) {
    return_str = _compare_obj.getHours() + ':' + (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes())
  } else if (unixStamp >= stamp[2] && unixStamp < stamp[1]) {
    const yesterdayText = '昨天'
    return_str = yesterdayText + ' ' + _compare_obj.getHours() + ':' +
      (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes())
  } else if (unixStamp >= stamp[3] && unixStamp < stamp[2]) {
    const theDayBeforeYesterdayText = '前天'
    return_str = theDayBeforeYesterdayText + ' ' + _compare_obj.getHours() + ':' +
      (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes())
  } else if (unixStamp >= stamp[4] && unixStamp < stamp[3]) { // 7天内
    const daynames = ['天', '一', '二', '三', '四', '五', '六']
    const dathStr = '星期' + daynames[_compare_obj.getDay()]

    const SundayText = '星期天'
    const MondayText = '星期一'
    const TuesdayText = '星期二'
    const WednesdayText = '星期三'
    const ThursdayText = '星期四'
    const FridayText = '星期五'
    const SaturdayText = '星期六'

    let dathStr2

    switch (dathStr) {
      case '星期天':
        dathStr2 = SundayText
        break
      case '星期一':
        dathStr2 = MondayText
        break
      case '星期二':
        dathStr2 = TuesdayText
        break
      case '星期三':
        dathStr2 = WednesdayText
        break
      case '星期四':
        dathStr2 = ThursdayText
        break
      case '星期五':
        dathStr2 = FridayText
        break
      case '星期六':
        dathStr2 = SaturdayText
        break
      default:
        dathStr2 = dathStr
        break
    }

    return_str = dathStr2 + ' ' + _compare_obj.getHours() + ':' +
      (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes())
  } else if (unixStamp >= stamp[5] && unixStamp < stamp[4]) { // 365天内
    const monthText = '月'
    const dayText = '日'
    // eslint-disable-next-line camelcase
    return_str = (_compare_obj.getMonth() - (-1)) + monthText + _compare_obj.getDate() + dayText + ' ' +
      _compare_obj.getHours() + ':' + (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes())
  } else {
    const yearText = '年'
    const monthText = '月'
    const dayText = '日'
    return_str = _compare_obj.getFullYear() + yearText + (_compare_obj.getMonth() - (-1)) +
      monthText + _compare_obj.getDate() + dayText + ' ' + _compare_obj.getHours() + ':' +
      (_compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes())
  }
  return return_str
}


export const UUID=()=>{
  const s = [] as Array<any>;
  const hexDigits = "0123456789abcdef";
  for (let i = 0; i < 20; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  return s.join("");
}
